﻿using System.Web;
using System.Web.Security;
using RuTracker.Interface.Login;
using RuTracker.MVC.Core;

namespace RuTracker.MVC.Models.Login
{
    public class LoginProcessing
    {
        public LoginProcessing(HttpRequestBase request, HttpResponseBase response)
        {
            Response = response;
            Request = request;
        }

        
        public LoginResults ProcessLogin(LoginModel loginModel, LoginResults lastLoginResults)
        {
            ILoginProcessor processor = Container.Current.Resolve<ILoginProcessor>();

            LoginResults loginResults = lastLoginResults != null && lastLoginResults.IsCaptchaRequired
                ? processor.LoginWithCaptcha(loginModel.UserName, loginModel.Password, lastLoginResults.CaptchaCode, loginModel.CaptchaCodeValue, lastLoginResults.CaptchaSid) 
                : processor.Login(loginModel.UserName, loginModel.Password);

            if (loginResults.Succeeded)
                FormsAuthentication.SetAuthCookie(loginModel.UserName, true);
            
            return loginResults;
        }

        private HttpRequestBase Request { get; set; }
        private HttpResponseBase Response { get; set; }
    }
}